package com.tueye.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.tueye.entity.Admin;
import com.tueye.utils.TuEyeConstant;

/**
 * 	1,preHandle()方法在Controller执行前调用，其返回值指定了是否应当继续处理请求。若返回false，Spring MVC框架将不再继续调用下一个拦截器，也不会将请求交给Controller处理,<br>
 *    整个请求处理将到此结束。<br>
 * 	2,postHandler()方法在Controller执行完毕后调用，此时Controller仅返回了ModelAndView对象，还没有对视图进行渲染，在这个方法中有机会对ModelAndView进行修改。<br>
 * 
 *	3,afterCompletion()方法在整个请求全部完成后调用，通过判断参数ex是否为null就可以判断是否产生了异常。<br>
 *
 * @author hanxiaowei
 *
 */
public class TuEyeHandlerInterceptor implements HandlerInterceptor{
	
	private final Log log = LogFactory.getLog(getClass());
	
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		//保留待处理........
		log.info("class TuEyeHandlerInterceptor  is method -- afterCompletion is Exception --");
		 ex.printStackTrace();
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler, ModelAndView mav) throws Exception {
		//保留待处理........
		 
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		//保留待完善.........
		log.info("spring 拦截器启动...............");
		boolean retuln = true;
		Admin admin = (Admin)request.getSession().getAttribute(TuEyeConstant.TUEYE_ADMIN);
		if(admin==null)retuln = false;
		
		return retuln;
	}

}
